package com.luckberry.murder.controller.api;

import com.alibaba.fastjson.JSON;
import com.luckberry.murder.entity.game.Appraise;
import com.luckberry.murder.entity.game.Game;
import com.luckberry.murder.entity.game.Participant;
import com.luckberry.murder.service.GameService;
import com.luckberry.murder.util.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/api")
public class ApiGameController {

    @Resource
    private GameService service;

    /**
     * game
     */
    @ResponseBody
    @RequestMapping(value = "/games", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getGames() {
        return JSON.toJSONString(service.getGames());
    }

    @ResponseBody
    @RequestMapping(value = "/game/{uid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getGame(@PathVariable("uid") int uid) {
        return JSON.toJSONString(service.getGameByUser(uid));
    }

    @ResponseBody
    @RequestMapping(value = "/game/exist", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String isExist(int gid) {
        return JSONUtil.result(service.getGame(gid) != null);
    }

    @ResponseBody
    @RequestMapping(value = "/game", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addGame(@RequestBody String json) {
        return JSONUtil.result(service.addGame(JSON.parseObject(json, Game.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/game/{gid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteGame(@PathVariable("gid") int gid) {
        return JSONUtil.result(service.deleteGame(gid));
    }

    @ResponseBody
    @RequestMapping(value = "/game/user", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteGameByUser(int uid) {
        return JSONUtil.result(service.deleteGameByUser(uid));
    }

    @ResponseBody
    @RequestMapping(value = "/game", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editGame(@RequestBody String json) {
        return JSONUtil.result(service.editGame(JSON.parseObject(json, Game.class)));
    }

    /**
     * appraise
     */
    @ResponseBody
    @RequestMapping(value = "/appraises", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getAppraises() {
        return JSON.toJSONString(service.getAppraises());
    }

    @ResponseBody
    @RequestMapping(value = "/appraise/{aid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getAppraise(@PathVariable("aid") int aid) {
        return JSON.toJSONString(service.getAppraise(aid));
    }

    @ResponseBody
    @RequestMapping(value = "/appraise", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addAppraise(@RequestBody String json) {
        return JSONUtil.result(service.addAppraise(JSON.parseObject(json, Appraise.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/appraise/{aid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteAppraise(@PathVariable("aid") int aid) {
        return JSONUtil.result(service.deleteAppraise(aid));
    }

    @ResponseBody
    @RequestMapping(value = "/appraise", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editAppraise(@RequestBody String json) {
        return JSONUtil.result(service.editAppraise(JSON.parseObject(json, Appraise.class)));
    }

    /**
     * participant
     */
    @ResponseBody
    @RequestMapping(value = "/participants", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getParticipants() {
        return JSON.toJSONString(service.getParticipants());
    }

    @ResponseBody
    @RequestMapping(value = "/participants/game", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getParticipantByGame(int gid) {
        return JSON.toJSONString(service.getParticipantsByGame(gid));
    }

    @ResponseBody
    @RequestMapping(value = "/participant/joined", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String joined(int uid) {
        return JSONUtil.result(service.getParticipantByUser(uid) != null);
    }

    @ResponseBody
    @RequestMapping(value = "/participant", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addParticipant(@RequestBody String json) {
        return JSONUtil.result(service.addParticipant(JSON.parseObject(json, Participant.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/participant/user", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteParticipantByUser(int uid) {
        return JSONUtil.result(service.deleteParticipantByUser(uid));
    }

    @ResponseBody
    @RequestMapping(value = "/participant/game", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteParticipantByGame(int gid) {
        return JSONUtil.result(service.deleteParticipantByGame(gid));
    }
}
